package com.leapcloud.crm.core.model;

import com.leapcloud.crm.core.utils.JsonUtils;
import com.leapcloud.crm.core.vo.PageVO;
import lombok.Data;
import org.jooq.Condition;
import org.jooq.SortField;

import java.util.List;

/**
 * @author motorfu
 * @email ffu@maxleap.com
 * @since 2018/8/29 16:39
 * @see com.leapcloud.ims.core.builder.Page
 */
@Deprecated
@Data
public class Page extends MBean {
  private int pageNum = 1;
  private int pageSize = 15;

  private Condition condition;

  private List<SortField<?>> orderFields;

  public Page() {
  }

  public Page(Builder builder) {
    this.pageNum = builder.pageNum;
    this.pageSize = builder.pageSize;
    this.orderFields = builder.orderFields;
    this.condition = builder.condition;
  }

  /**
   * 使用 Page.newBuilder(pageVO).build();
   *
   * @param pageVO
   */
  @Deprecated
  public Page(PageVO pageVO) {
    this.pageNum = pageVO.getPageNum();
    this.pageSize = pageVO.getPageSize();
    this.orderFields = pageVO.getOrderFields();
  }

  /**
   * 使用 Page.newBuilder(pageVO, condition).build();
   *
   * @param pageVO
   * @param condition
   */
  @Deprecated
  public Page(PageVO pageVO, Condition condition) {
    this(pageVO);
    this.condition = condition;
  }

  public static Builder newBuilder(PageVO pageVO) {
    return new Builder(pageVO);
  }


  public static class Builder {
    private int pageNum = 1;
    private int pageSize = 15;

    private Condition condition;

    private List<SortField<?>> orderFields;

    public Builder(PageVO pageVO) {
      this.pageNum = pageVO.getPageNum();
      this.pageSize = pageVO.getPageSize();
      this.orderFields = pageVO.getOrderFields();
    }

    public Builder condition(Condition condition) {
      this.condition = condition;
      return this;
    }

    public Page build() {
      return new Page(this);
    }

    public String toJson() {
      return JsonUtils.encode(new Page(this));
    }
  }
}
